Shader "Unlit/Reflect1"
{
    Properties
    {
        _CubeMap ("CubeMap", Cube) = "white" {}
        _ReflectActivity("ReflectActivity", Range(0, 1)) = 1
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Opaque" "Queue"="Geometry"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                // 在顶点着色器阶段算出来的 方向向量用于在立方体纹理进行采样
                float3 reflectDir : TEXCOORD0;
            };

            samplerCUBE _CubeMap;
            float _ReflectActivity;


            v2f vert(appdata_base v)
            {
                v2f data;
                data.vertex = UnityObjectToClipPos(v.vertex);
                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                float3 cameraDir = normalize(worldPos - _WorldSpaceCameraPos).xyz;
                float3 reflectDir = reflect(cameraDir, worldNormal);
                data.reflectDir = reflectDir;
                return data;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                //1.在立方体纹理中进行采样-->得到颜色
                fixed4 reflectColor = texCUBE(_CubeMap, i.reflectDir) * _ReflectActivity;

                return reflectColor;
            }
            ENDCG
        }
    }
}